package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.androidnetframe.utils.LogUtils;
import com.androidnetframe.utils.UIUtils;
import com.mmlc.inter.InterActivity;
import com.mmlc.inter.MyReceiver;
import com.mmlc.meimei.lichengkongjian.App;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.classify.OrderFormActivity;
import com.mmlc.meimei.lichengkongjian.classify.PayResultActivity;
import com.mmlc.meimei.lichengkongjian.customview.PhoneEditText;
import com.mmlc.meimei.lichengkongjian.shoppingbag.ShoppingCartActivity;
import com.mmlc.network.filter.LoginRequestFilter;
import com.mmlc.network.responsebean.LoginResponseBean;
import com.mmlc.utils.ActivityManager;
import com.mmlc.utils.EditInputUtils;
import com.mmlc.utils.PhoneUtils;
import com.mmlc.utils.ToastUtils;

public class LoginActivity extends InterActivity implements OnClickListener {
    PhoneEditText et_phoneNumber;
    EditText et_password;
    Button bt_login;
    ViewGroup rootView;
    TextView tv_register;
    TextView tv_forgetPassword;
    LinearLayout ll_qq;
    LinearLayout ll_weixin;
    LinearLayout ll_weibo;
    TextView tv_title;
    ImageView iv_back;
    @Override
    protected void initView() {
        et_phoneNumber = (PhoneEditText) findViewById(R.id.et_phoneNumber);
        et_password = (EditText) findViewById(R.id.et_password);
        bt_login = (Button) findViewById(R.id.bt_login);
        rootView = (ViewGroup) findViewById(R.id.rootView);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        tv_register = (TextView) findViewById(R.id.tv_register);
        tv_forgetPassword = (TextView) findViewById(R.id.tv_forgetPassword);
        ll_qq = (LinearLayout) findViewById(R.id.ll_qq);
        ll_weixin = (LinearLayout) findViewById(R.id.ll_weixin);
        ll_weibo = (LinearLayout) findViewById(R.id.ll_weibo);
        com.mmlc.utils.UIUtils.updateTextSize(et_phoneNumber,"请输入手机号");
        com.mmlc.utils.UIUtils.updateTextSize(et_password,"请输入密码");
        processData();
    }

    @Override
    protected void initEvent() {

    }
    String flag;
    public void processData() {

        flag = getIntent().getStringExtra("LoginOut");
        //处理标题栏
        tv_title.setText("用户登录");
        iv_back.setVisibility(View.INVISIBLE);
        //登陆
        processLogin();
        //注册
        tv_register.setOnClickListener(this);
        //忘记密码
        tv_forgetPassword.setOnClickListener(this);
        //QQ
        ll_qq.setOnClickListener(this);
        //微信
        ll_weixin.setOnClickListener(this);
        //微博
        ll_weibo.setOnClickListener(this);
    }

    private void processLogin() {
        bt_login.setOnClickListener(this);
    }

    @Override
    protected int initContentView() {
        return R.layout.fragment_login;
    }

    @Override
    protected void initExtra() {
        super.initExtra();
/*
        umShareAPI = MainActivity.umShareAPI;*/
        //UMShareAPI.get(getActivity());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                com.mmlc.utils.UIUtils.hideKeyBoard(this,et_phoneNumber);
                com.mmlc.utils.UIUtils.hideKeyBoard(this,et_password);
                requestLogin();
                break;
            case R.id.tv_register:
                UIUtils.simpleStartActivity(LoginActivity.this, RegisterAccount.class);
                break;
            case R.id.ll_qq:
                break;
            case R.id.tv_forgetPassword:
                Intent intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_weixin:
                break;
            case R.id.ll_weibo:
                break;
        }
    }

    private void requestLogin() {
        //检查手机号的合法性
        boolean isPhone = EditInputUtils.checkedPhoneNumber(et_phoneNumber.getText().toString());
        if (!isPhone) {
            ToastUtils.showToast(LoginActivity.this, "请输入正确的手机号");
            return;
        }
        //检查密码是否符合要求
        boolean isPassword = EditInputUtils.checkPassword(LoginActivity.this, et_password.getText().toString());
        if (!isPassword) {
            return;
        }
        //发送登陆的网络请求
        final LoginRequestFilter filter = new LoginRequestFilter(LoginActivity.this);
        filter.requestBean.method = "login";
        filter.requestBean.phone = et_phoneNumber.getInputText();
        filter.requestBean.password = et_password.getText().toString();
        filter.requestBean.client_id = MyReceiver.getResId(LoginActivity.this);
        filter.requestBean.imei = PhoneUtils.getIMEI(LoginActivity.this);
        filter.offerErrorParams(rootView);
        filter.isTransparence = true;
        filter.setDebug(false);//表示请求真正的服务端接口
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<LoginResponseBean>() {

            @Override
            public void onSuccess(LoginResponseBean result) {
                //缓存用户信息
                App.user.setPhone(et_phoneNumber.getInputText());
                App.user.setPassword(et_password.getText().toString());
                App.user.savePhoto(result.getData().getHeader_thumb());
                if(!TextUtils.isEmpty(result.getData().getNichen())){
                    App.user.saveNickName(result.getData().getNichen());
                }else{
                    App.user.saveNickName("");
                }
                if(!TextUtils.isEmpty(result.getData().getSex())){
                    App.user.saveSex(Integer.parseInt(result.getData().getSex()));
                }

                App.user.setLoginState(true);
                App.user.setUserType(result.getData().getType());
                App.user.setUserId(result.getData().getId());
                App.user.setIsStore(result.getData().getIs_store());
                App.user.setIsTuiDan(result.getData().getIs_tuidan());
                /*App.user.saveNickName();
                App.user.setPassword();*/
                String type = getIntent().getStringExtra("type");
                if(!TextUtils.isEmpty(type)){
                    if(type.equals("service") || type.equals("pay")|| type.equals("count")
                            || type.equals("like") || type.equals("add") || type.equals("replace")){
                        //判断从哪里跳转到登录界面
                    }
                }else {
                    //进入个人中心列表界面
//                    ActivityManager.finishActivity(MainActivity.class);
//                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                    intent.putExtra("LoginOut", flag);
//                    startActivity(intent);
//                    sendBroadcast(new Intent("com.mmlc.meimei.meiqi.LoginSuccess"));
                }
                finish();
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                App.user.setLoginState(false);
                LogUtils.e("123",serverError.mess.toString());
                ToastUtils.showToast(serverError.mess);
            }
        });
    }

    @Override
    public void finish() {
        if (!App.user.getLoginState()) {
            sendBroadcast(new Intent("com.mmlc.meimei.meiqi.LoginFailure"));
            ActivityManager.finishActivity(ShoppingCartActivity.class);
            ActivityManager.finishActivity(OrderFormActivity.class);
            ActivityManager.finishActivity(PayResultActivity.class);
        }
        super.finish();
    }
//
//    @Override
//    public void onBackPressed() {
////        ActivityManager.finishActivity(MainActivity.class);
////        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
////        intent.putExtra("LoginOut",flag);
////        startActivity(intent);
////        ActivityManager.finishActivity(LoginActivity.class);
////        super.onBackPressed();
//    }

}
